const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    s: app.system.statusBarHeight, //状态栏高度
    n: (app.menu.top - app.system.statusBarHeight) * 2 + app.menu.height, //导航栏高度
    h: app.menu.height, //胶囊高度
    tabs: [],
    activeIndex: 0,//选择的下标
    weChatPopup: null,//添加微信
    viewsnum: 0,
    Procurementnum: 0,
    Sourcenum: 0,
    productsnum: 0,
    views: 0,//浏览数
    Procurement: 0,//采购数字
    Source: 0,//货源
    products: 0,//产品
    duration: 3000,
    page: 1,//页码
    pagingState: false,//分页状态
    articleList: [],//商圈列表
    purchasepage: 1,//页码
    purchaseState: false,//分页状态
    purchaseList: [],//商圈列表
    bannerList: [],//轮播图列表
    businessConfig: {},//商圈配置
    isenabled: false,//下拉刷新状态
    keyword: '',//搜索关键字
    useritem: {},//弹框内容
    userinfo: getApp().userinfo
  },
  /**搜索 */
  sous() {
    let { tabs, activeIndex } = this.data
    if (activeIndex == 0) {
      this.setData({
        page: 1,//页码
        pagingState: false,//分页状态
        articleList: [],//商圈列表
      })
    } else {
      this.setData({
        purchasepage: 1,//页码
        purchaseState: false,//分页状态
        purchaseList: [],//商圈列表
      })
    }
    this.articleLists(tabs[activeIndex])
    // this.articleLists()
  },
  /**清空 */
  clearKeyword() {
    let { tabs, activeIndex } = this.data
    if (activeIndex == 0) {
      this.setData({
        keyword: '',
        page: 1,//页码
        pagingState: false,//分页状态
        articleList: [],//商圈列表
      })
    } else {
      this.setData({
        keyword: '',
        purchasepage: 1,//页码
        purchaseState: false,//分页状态
        purchaseList: [],//商圈列表
      })
    }
    this.articleLists(tabs[activeIndex])
  },
  /**双向数据榜单 */
  inputCheng(event) {
    console.log(event);
    this.setData({
      keyword: event.detail.value
    })
  },
  /**获取商圈上传配置 */
  async getExhibitionConfig() {
    const { code, data } = await getApp().api.getExhibitionConfig()
    if (code == 1) {
      this.setData({
        businessConfig: data
      })
      getApp().local.set('businessConfig', data)
    }
  },
  /**看吧信息 */
  async boardInfo() {
    const { code, data } = await getApp().api.boardInfo()
    if (code == 1) {
      this.setData({
        views: data.show_nums,
        Procurement: data.buy_nums,
        Source: data.want_nums,
        products: data.count,
      })
    }
  },
  /**关注取下关注 */
  async communityfollow(event) {
    console.log(event);
    let { item, index } = event.currentTarget.dataset
    let object = {
      follow_id: item.user_id,
      status: item.is_follow == 0 ? '1' : '0'
    }
    const { code, data } = await getApp().api.communityfollow(object)
    if (code == 1) {
      this.data.articleList[index].is_follow = item.is_follow == 0 ? '1' : '0'
      this.setData({
        articleList: this.data.articleList
      })
    }
  },
  /**广告位 */
  async getPositionList() {
    let object = {
      ad_sn: 'businessBanner'
    }
    const { code, data } = await getApp().api.getPositionList(object)
    if (code == 1) {
      this.setData({
        bannerList: data
      })
    }
  },
  /**图片预览 */
  previewImage(event) {
    let { index, imagelist } = event.currentTarget.dataset
    let image = imagelist.map(item => item.image)
    wx.previewImage({
      current: image[index],
      urls: image
    });
  },
  /**文本隐藏展示 */
  toggleText(event) {
    console.log(this.data.articleList);
    let { index } = event.currentTarget.dataset
    this.data.articleList[index].showText = !this.data.articleList[index].showText
    this.setData({
      articleList: this.data.articleList,
    });
  },
  /**获取商圈列表 */
  async articleLists(item) {

    /**货源信息 */
    if (item.id == 2) {
      let object = {
        page_no: this.data.page,
        cate_id: item.id,
        keyword: this.data.keyword
      }
      if (!this.data.pagingState) {
        const { code, data } = await getApp().api.articleLists(object)
        if (code == 1) {
          this.setData({
            isenabled: false,
            articleList: this.data.articleList.concat(data.list).map(item => {
              item.create_time = getApp().friendlyFormatTime(item.create_time * 1000)
              item.showText = false
              return item
            })
          })
          if (data.more != 1) {
            this.setData({
              pagingState: true
            })
            return
          }
          this.setData({
            page: (this.data.page * 1) + 1
          })
          console.log(this.data.page);
        }
      }
    }
    /**采购信息 */
    if (item.id == 1) {
      let object = {
        page_no: this.data.purchasepage,
        cate_id: item.id,
        keyword: this.data.keyword
      }
      if (!this.data.purchaseState) {
        const { code, data } = await getApp().api.articleLists(object)
        if (code == 1) {
          this.setData({
            isenabled: false,
            purchaseList: this.data.purchaseList.concat(data.list).map(item => {
              item.create_time = getApp().friendlyFormatTime(item.create_time * 1000)
              item.showText = false
              return item
            })
          })
          if (data.more != 1) {
            this.setData({
              purchaseState: true
            })
            return
          }
          this.setData({
            purchasepage: (this.data.purchasepage * 1) + 1
          })
        }
      }
    }
  },
  /** 获取分类类别*/
  async communityCate() {
    const { code, data } = await getApp().api.communityCate()
    if (code == 1) {
      this.setData({
        tabs: data
      })
      for (let index = 0; index < data.length; index++) {
        const element = data[index];
        this.articleLists(element)
      }

    }
  },
  /** 打开更多弹框 */
  openPopup(event) {
    let { type, item } = event.currentTarget.dataset
    this.setData({
      useritem: item
    })
    this.data.weChatPopup.opneWeChat();
  },
  /**下滑倒地 */
  scrolltolower(event) {
    console.log(event);
    let { tabs, activeIndex } = this.data
    this.articleLists(tabs[activeIndex])
  },
  /** 跳转*/
  navigateTo(event) {
    let url = event.currentTarget.dataset.url
    console.log(url);
    wx.navigateTo({
      url,
    })
  },
  /**下拉刷新 */
  refresherrefresh(e) {
    console.log(e);
    let { tabs, activeIndex } = this.data
    if (activeIndex == 0) {
      this.setData({
        page: 1,//页码
        pagingState: false,//分页状态
        articleList: [],//商圈列表
      })
    } else {
      this.setData({
        purchasepage: 1,//页码
        purchaseState: false,//分页状态
        purchaseList: [],//商圈列表
      })
    }
    this.articleLists(tabs[activeIndex])
  },
  /** 滑动切换 */
  changeItem(e) {
    console.log(e, 'changeItem');
    const index = e.detail.current;
    this.setData({
      activeIndex: index,
    });
  },
  /** tabs切换 */
  onTabTap(e) {
    const index = e.currentTarget.dataset.index;
    this.setData({
      activeIndex: index,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getExhibitionConfig()
    this.boardInfo()
    this.getPositionList()
    this.communityCate()
    const weChatPopup = this.selectComponent('#weChat-popup');
    if (weChatPopup) {
      this.setData({
        weChatPopup: weChatPopup
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})